<?php
namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\CouponAssignParams;
use App\Params\Backend\CouponListParams;
use App\Params\Backend\CouponUpdateParam;
use App\Services\Promotion\CouponService;
use App\Traits\VerifyRequestParam;

/**
 * 优惠券控制器
 *
 * @package App\Http\Controllers\Backend
 * @author WangBin
 * @date 2021-06/11
 */
class CouponController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 获取优惠券列表
     *
     * @return
     */
    public function getList()
    {
        $params = CouponListParams::create();
        $ret = CouponService::getInstance()->getList($params);
        return $this->success($ret);
    }

    /**
     * 保存优惠券
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function update()
    {
        $param = CouponUpdateParam::create();
        $ret = CouponService::getInstance()->update($param);
        if($ret) {
            return $this->success(['coupon_id' => $ret]);
        }
        return $this->fail(ResponseCode::SAVE_FAIL);
    }

    /**
     * 停启用优惠券
     *
     * @throws \App\Exceptions\BusinessException
     */
    public function enable()
    {
        CouponService::getInstance()->enable($this->verifyInteger('coupon_id'));
        return $this->success();
    }

    /**
     * 查看优惠券
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function get()
    {
        $ret = CouponService::getInstance()->get($this->verifyInteger('coupon_id'));
        return $this->success($ret);
    }

    /**
     * 发放优惠券
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function assign()
    {
        $param = CouponAssignParams::create();
        $ret = CouponService::getInstance()->saveAssignLog($param);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::OP_FAIL);
    }
}
